const App = getApp();

import moment from '../../utils/moment'
import Dialog from '../../weapp/dialog/dialog'
import Toast from '../../weapp/toast/toast';
// components/list/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    type: Number,
    styleP: Boolean,
    strs: String,
    cusPageIndex: Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    title: '我的客户',
    type: '',    //客户类型
    timeData: {},
    remind_time: '',
    deviceList: [],    //列表
    deviceTotal: 0,    //总数
    devicePages: 0,     //总页数
    deviceIndex: 1,      //当前页 
    searchLoading: false, //加载更多
    deviceNoData: false,  //暂无更多
    searchLoadingComplete: false, //加载到底部
    see_show: 0,
  },
  lifetimes: {
    ready: function () {
      // this.getCustomersList()
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onChange(e) {
      this.setData({
        timeData: e.detail,
      });
      // console.log(e)
    },
    handleHomeToReport(e) {
      let { type, id, status, reportid, remindid, project_name, status_true, nopass_text } = e.currentTarget.dataset
      // if (status == 0) {
      wx.navigateTo({
        url: `../../pages/report/index?type=${type}&id=${id}&reportid=${reportid}&status=${status}&remindid=${remindid}&project_name=${project_name}&status_true=${status_true}&nopass_text=${nopass_text}`,
      })
      // }
    },
    toDetail: function (e) {
      let id = e.currentTarget.dataset.id
      let inspect_id = e.currentTarget.dataset.inspectid
      let tabid = e.currentTarget.dataset.tabid
      let ismy = e.currentTarget.dataset.ismy
      console.log(inspect_id)
      if (this.data.type == 2) {
        wx.navigateTo({
          url: `../../pages/tabkelooklist/index?inspect_id=${inspect_id}&status=list`,
        })
      }else{
        if (ismy) {
          if (ismy == 1) {
            wx.navigateTo({
              url: `../../pages/detail/index?id=${id}&post_index=${this.data.cusPageIndex}&tabid=${tabid}`,
            })
          }
        }
        if (ismy == undefined) {
          wx.navigateTo({
            url: `../../pages/detail/index?id=${id}&post_index=${this.data.cusPageIndex}&tabid=${tabid}`,
          })
        }
      }
      
    },
    handleEditReoprt(e) {
      let id = e.currentTarget.dataset.remindid
      wx.navigateTo({
        url: `../../pages/report/editRep?id=${id}`,
      })
    },
    filterList(arr) {
      for (let index = 0; index < arr.length; index++) {
        arr[index].pic = ''
        arr[index].create_time = arr[index].create_time.slice(5)
        arr[index].create_time = arr[index].create_time.substring(0, arr[index].create_time.length - 3);
        // this.setData({
        //   time: arr[index].rollout_expire_time * 1000,
        //   remind_time: moment(arr[index].remind_time).format('M/DD')
        // })
        switch (arr[index].level) {
          case 'A':
            arr[index].pic = '../../images/A.png'
            break;
          case 'B':
            arr[index].pic = '../../images/B.png'
            break;
          case 'C':
            arr[index].pic = '../../images/C.png'
            break;
          case 'D':
            arr[index].pic = '../../images/D.png'
            break;
          case 'T':
            arr[index].pic = '../../images/T.png'
            break;
          case 'W':
            arr[index].pic = '../../images/W.png'
            break;
          default:
            break;
        }
      }
    },
    //协防客户按钮操作
    handleHelpSubmit(e) {
      let _this = this
      let { id, api, type } = e.currentTarget.dataset
      let obj = {}
      obj = api == 'confirmHelpDefense' ? { h_id: id, confirm_type: type } : { h_id: id }
      Dialog.confirm({
        title: '标题',
        message: '是否进行该操作？',
      })
        .then(() => {
          App._post_form(`HelpDefense/${api}`, obj, function (result) {
            if (result.code === 0) {
              Toast.success('提交成功！')
              _this.getXfCustList()
            } else {
              Toast.fail(result.msg)
            }
          })
        })
        .catch(() => {
          // on cancel
        });
    },
    mobileStr(v) {
      return v ? String(v).substring(0, 3) + "********" : ''
    },
    /**
     * 获取首页客户列表数据
     */
    getCustomersList(action) {
      // console.log(this.data.cusPageIndex)
      console.log(action);
      let _this = this
      // wx.showLoading({
      //   title: '加载中...',
      // })
      App._post_form('home/getHomeListData', {
        page: _this.data.deviceIndex,
        action,
        type:1
      }, function (result) {
        console.log(result);
        // return false
        wx.hideLoading()
        _this.filterList(result.data.list)
        let Result = result.data.list
        // let Result = result.data.list.map(el=>{
        //           const mobile = _this.mobileStr(el.mobile);
        //   console.log(mobile )
        //           return {...el, mobile}
        //         })
        // 响应无数据 则提示无数据
        if (_this.data.deviceIndex == 1 && Result.length == 0) {
          _this.setData({
            deviceNoData: true,
            searchLoading: false,
            searchLoadingComplete: false
          })
        }
        if (Result.length) {
          _this.data.deviceTotal = result.data.count
          _this.data.devicePages = result.data.total_page
          let deviceListTemp = [];
          if (_this.data.deviceIndex > 1) {
            deviceListTemp = _this.data.deviceList.concat(Result);
            // var result = [];
            // var obj = {};
            // for (var i = 0; i < deviceListTemp.length; i++) {
            //   if (!obj[deviceListTemp[i].id]) {
            //     result.push(deviceListTemp[i]);
            //     obj[deviceListTemp[i].id] = true;
            //   }
            // }
            // deviceListTemp = result
            console.log(deviceListTemp)
            _this.setData({
              searchLoading: true, //"正在载入更多"显示
            });
          } else {
            deviceListTemp = Result;
          }
          _this.setData({
            deviceList: deviceListTemp, //获取数据数组
            deviceNoData: false //“没有数据”隐藏
          });
        }
      });
    },
    /**
     * 获取客户页客户列表数据
     */
    // getCustList(action) {
    //   let _this = this
    //   wx.showLoading({
    //     title: '加载中...',
    //   })
    //   // console.log(_this.data.deviceIndex);
    //   App._post_form('home/getHomeListData', {
    //     action,
    //     page: _this.data.deviceIndex,
    //   }, function (result) {
    //     wx.hideLoading()
    //     _this.filterList(result.data.list)
    //     let Result = result.data.list
    //     //     let Result = result.data.list.map(el=>{
    //     //         const mobile = _this.mobileStr(el.mobile);
    //     //         return {...el, mobile}
    //     //      })
    //     // 响应无数据 则提示无数据
    //     if (_this.data.deviceIndex == 1 && Result.length == 0) {
    //       _this.setData({
    //         deviceNoData: true,
    //         searchLoading: false,
    //         searchLoadingComplete: false
    //       })
    //     }
    //     if (Result.length) {
    //       _this.data.deviceTotal = result.data.count
    //       _this.data.devicePages = result.data.total_page
    //       let deviceListTemp = [];
    //       if (_this.data.deviceIndex > 1) {
    //         deviceListTemp = _this.data.deviceList.concat(Result);
    //         var result = [];
    //         var obj = {};
    //         for (var i = 0; i < deviceListTemp.length; i++) {
    //           if (!obj[deviceListTemp[i].id]) {
    //             result.push(deviceListTemp[i]);
    //             obj[deviceListTemp[i].id] = true;
    //           }
    //         }
    //         console.log(deviceListTemp)
    //         _this.setData({
    //           searchLoading: true, //"正在载入更多"显示
    //         });
    //       } else {
    //         deviceListTemp = Result;
    //       }
    //       _this.setData({
    //         deviceList: deviceListTemp, //获取数据数组
    //         deviceNoData: false //“没有数据”隐藏
    //       });
    //     }
    //   });
    // },
    //删除带看
    deleteInspect(e) {
      let _this = this
      wx.showModal({
        title: '是否删除?',
        success: (res) => {
          if (res.confirm) {
            App._post('customer/inspectDelete', {
              remind_id: e.currentTarget.dataset.id
            }, function (result) {
              if (result.code === 0) {
                Toast.success(result.msg)
                _this.getCustomersList('inspect')
              } else {
                console.log('1111')
                Toast.fail(result.msg)
              }
            })
          }
        }
      })
    },
    // 页面触底时执行
    reachBottom(action) {
      var _this = this;
      if (_this.data.deviceIndex < _this.data.devicePages) {
        _this.setData({
          deviceIndex: _this.data.deviceIndex + 1,   //每次触发触底事件，把deviceIndex+1
        });
        if (_this.data.strs == 'home_page') {
          _this.getCustomersList(action);
          return
        }

      } else if (_this.data.deviceIndex == _this.data.devicePages) {
        _this.setData({
          searchLoadingComplete: true, //“已加载全部”显示
          searchLoading: false, //"正在载入更多"隐藏
        });
      }
    }
  }
})
